package com.zhentao.util;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zhentao.userLocal.UserThreadLocalUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @ClassName: MyMetaObject
 * @Author: muhaoyang1
 * @Date: 2024/12/19 9:48
 * @Comment:添加/修改时 插入值
 */
@Component
public class MyMetaObject {

    @Bean
    public MetaObjectHandler metaObjectHandler(){
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
                this.strictInsertFill(metaObject, "createBy", Long.class, UserThreadLocalUtil.getUser());
                this.strictInsertFill(metaObject, "isDel", Integer.class, 1);
            }
            @Override
            public void updateFill(MetaObject metaObject) {
                this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
                this.strictUpdateFill(metaObject, "updateBy",Long.class, UserThreadLocalUtil.getUser());
            }
        };
    }
}
